package member

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	auth_user "go-papers/common/auth-user"

	"go-papers/models"
	"go-papers/tools"
	"go-papers/tools/app"
	"go-papers/tools/app/msg"
)

func GetMemberCollectList(c *gin.Context) {
	var data models.MemberCollect
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}
	if collectId := c.Request.FormValue("collectId"); collectId != "" {
		data.CollectId = tools.StrToInt(err, collectId)
	}
	data.OpenId = c.Request.FormValue("openId")
	data.MemberId = tools.GetMemberId(c)
	data.ThirdUserId = auth_user.GetThirdUserId(c, data.OpenId)
	if ok, msgs := auth_user.VerifyUid(c, data.MemberId, data.ThirdUserId); ok {
		app.CommonResp(c, 126, msgs, nil)
		return
	}

	if courseId := c.Request.FormValue("courseId"); courseId != "" {
		data.CourseId = tools.StrToInt(err, courseId)
	}
	data.CourseName = c.Request.FormValue("courseName")

	result, count, err := data.GetPage(pageSize, pageIndex)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetMemberCollect(c *gin.Context) {
	var data models.MemberCollect
	data.CollectId, _ = tools.StringToInt(c.Param("collectId"))
	data.MemberId = tools.GetMemberId(c)
	data.ThirdUserId = auth_user.GetThirdUserId(c, data.OpenId)

	result, err := data.Get()
	if err != nil {
		app.ApiResp(c, -2, "抱歉未找到相关信息："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func InsertMemberCollect(c *gin.Context) {
	var data models.MemberCollect
	err := c.ShouldBindJSON(&data)
	tools.HasError(err, "数据异常", -1)

	data.MemberId = tools.GetMemberId(c)
	data.ThirdUserId = auth_user.GetThirdUserId(c, data.OpenId)

	if ok, msgs := auth_user.VerifyUid(c, data.MemberId, data.ThirdUserId); ok {
		app.CommonResp(c, 126, msgs, nil)
		return
	}

	sr, _ := data.GetV2()
	if sr.CollectId != 0 {
		app.CommonResp(c, 0, "记录已经存在", sr)
		return
	}
	result, err := data.Create()
	tools.HasError(err, "加入失败", -1)
	app.OK(c, result, "成功")
}

func UpdateMemberCollect(c *gin.Context) {
	var data models.MemberCollect
	err := c.BindWith(&data, binding.JSON)

	data.MemberId = tools.GetMemberId(c)
	data.ThirdUserId = auth_user.GetThirdUserId(c, data.OpenId)

	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), nil)
		return
	}

	result, err := data.Update(data.CollectId)
	if err != nil {
		app.ApiResp(c, -1, "数据操作失败："+err.Error(), nil)
		return
	}

	app.OK(c, result, "")
}

func DeleteMemberCollect(c *gin.Context) {
	var data models.MemberCollect

	data.OpenId = c.Request.FormValue("openId")
	data.MemberId = tools.GetMemberId(c)
	data.ThirdUserId = auth_user.GetThirdUserId(c, data.OpenId)
	collectIds := c.Request.FormValue("collectId")
	courseIds := c.Request.FormValue("courseId")

	if collectIds == "" && courseIds == "" {
		app.ApiResp(c, -1, "参数错误", nil)
		return
	}
	//IDS := tools.IdsStrToIdsIntGroupV2(collectIds)
	_, err := data.BatchDeleteV2(collectIds, courseIds)
	if err != nil {
		app.ApiResp(c, -1, "数据删除失败："+err.Error(), nil)
		return
	}

	app.OK(c, nil, msg.DeletedSuccess)
}
